<template>
    <div>
        <div>
            <el-form :model="valueList">
                    <el-form-item label="设备类型:" label-width="100px">
                        <el-cascader
                            ref="modecascader"
                            v-model="value"
                            :key="keyC"
                            :options="itemlistData"
                            :props="modecasProps"
                            :placeholder="valueList.valueName"
                            :disabled="linkItemTitle == '编辑' ? true :false"
                            @change="itemChange">
                        </el-cascader>
                    </el-form-item>
                    <div class="link_numput">
                        <el-form-item label="触发门限:" label-width="100px">
                            <el-input v-model="valueList.high" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="恢复门限:" label-width="100px">
                            <el-input v-model="valueList.low" autocomplete="off"></el-input>
                        </el-form-item>
                    </div>
                    <div class="link_numput">
                        <el-form-item label="延迟保存(s):" label-width="100px">
                            <el-input v-model="valueList.delay" autocomplete="off" type="number" ></el-input>
                        </el-form-item>
                        <p class="tishi">*当前值在触发门限状态下一直维持到延迟保存时间为止，当前触发条件方可生效。</p>
                    </div>
                </el-form>
                <div class="linkson_save">
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="save(0)">取 消</el-button>
                        <el-button type="primary" @click="save(1)">确 定</el-button>
                    </span>
                </div>
        </div>
    </div>
</template>
<script>
export default {
    name:'Cascaderitem',
    props: ['valueList','itemlistData','linkItemTitle'],
    data() {
        return {
            value:'',
            keyC:0,
            addr:0,
            modecasProps: {
                multiple: false, //多选
                value: 'value',
                label: 'label',
                addr: 'addr',
                // 懒加载
                lazy: true, // 必须为true
                lazyLoad:this.lazyLoad,
            },
        }
    },
    created() {
    },
    mounted(){
        this.$nextTick(function() {
            this.$on('getKeya', function() {
                this.keyC++
            });
        });
    },
    methods: {
        save(val){
            this.keyC++
            var data  = []
            if(val==1){//保存
                if(this.valueList.valueName == '请选择'){
                    this.$notify({
                        title: '未选择设备',
                        type: 'warning',
                        duration: 1500
                    });
                }else{
                    console.log(this.valueList)
                    if(this.valueList.delay > 60){
                        this.$notify({
                            title: '延迟时间不能超过1分钟',
                            type: 'warning',
                            duration: 1500
                        });
                        return false;
                    }
                    this.valueList.low = Number(this.valueList.low)
                    this.valueList.high = Number(this.valueList.high)
                    this.valueList.delay = Number(this.valueList.delay)
                    data.push(val)
                    data.push(this.valueList)
                    this.$emit('childa', data);  
                    this.value = ''
                }
                
            }else if(val==0){//取消
                data.push(val)
                this.$emit('childa', data);  
                this.value = ''
            }
            
        },
        // 区分设备和自定义模块的编号
        itemChange(data){
            var pathvalue = this.$refs.modecascader.getCheckedNodes()[0].pathLabels;
            this.valueList.valueName = pathvalue.join("/")
            this.valueList.moduleName = pathvalue[0]
            if(data[0] == 'mode'){
                this.valueList.devType = 0
                this.valueList.moduleType = "mode"
                this.valueList.devUid = "0"
                this.valueList.devName = pathvalue[0]
                this.valueList.id = data[1]
                this.valueList.idName = pathvalue[1]
            }else{
                this.valueList.devType = data[0]
                this.valueList.moduleType = data[0].toString()
                this.valueList.devUid = data[1]
                this.valueList.devName = pathvalue[1]
                this.valueList.devAddr = this.addr
                this.valueList.id = data[2]
                this.valueList.idName = pathvalue[2]
            }
            
        },
        // 懒加载获取数据 
        lazyLoad(node,resolve){
            const level = node.level
            const type = node.path
            var unit = []
            let data
            if(level > 0 && type[0] === 'mode'){
                data = unit
                data.forEach(item => {
                    item.value = item.value
                    item.label = item.label
                    //将相关值赋值到选择器上
                    item.leaf = level >= 1
                })
                resolve(data)
                // 新增对最后一级的下一级的处理
                // 到最后一级的时候消除加载圈及禁止继续请求
                if (level === 2) {
                    resolve(data)
                    return
                }
            }else{
                if(node.data != undefined){
                    this.addr = Number(node.data.addr)
                }
                data = unit
                data.forEach(item => {
                    item.value = item.value
                    item.label = item.label
                    item.addr = item.addr
                    //将相关值赋值到选择器上
                    item.leaf = level >= 2
                })
                resolve(data)
                if (level >= 2) {
                    //接口获取
                    this.$http
                    .post(this.$baseUrl, {
                        "getdevinfo": [
                            {
                                "uid":type[1].toString(),
                                "type":Number(type[0]),
                                "addr":Number(node.data.addr)
                            }
                        ],
                        [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
                    }).then((res) => {
                        var itemData = res.data.getdevinfo[0].unit
                        for(var b = 0; b < itemData.length; b++){
                            if(itemData[b].ready == 1){
                                var reg = ''
                                if(itemData[b].id <= 34){
                                    reg = '(reg'+(itemData[b].id+2)+')'
                                }else if(itemData[b].id >= 35 && itemData[b].id <= 50){
                                    reg = '(reg37bit'+(itemData[b].id-35)+')'
                                }else if(itemData[b].id >= 51 && itemData[b].id <= 66){
                                    reg = '(reg38bit'+(itemData[b].id-51)+')'
                                }else if(itemData[b].id >= 67 && itemData[b].id <= 82){
                                    reg = '(reg39bit'+(itemData[b].id-67)+')'
                                }
                                var arr = {
                                    value:itemData[b].id,
                                    label:itemData[b].name+reg,
                                }
                                unit.push(arr)
                            }
                        }
                        data = unit
                        data.forEach(item => {
                            item.value = item.value
                            item.label = item.label
                            //将相关值赋值到选择器上
                            item.leaf = level >= 2
                        })
                        resolve(data)
                        // 新增对最后一级的下一级的处理
                        // 到最后一级的时候消除加载圈及禁止继续请求
                        if (level === 3) {
                            resolve(data)
                            return
                        }
                    })
                }
                
            }
            
            
            
            
        }
    }
}
</script>

<style scoped>
.el-cascader{
    width: 100%;
}
.link_numput{
    width: 100%;
    display: flex;
}
.linkson_save{
    text-align: right;
}
.el-button{
    color: #fff;
}
.tishi{
    font-size: 12px;
    padding-left: 10px;
    padding-top: 10px;
    color: #ff942a;
}
</style>